import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';

import '../const.dart';

class Utils {
  static final String GET = "GET";
  static final String POST = "POST";
  static final String DATA = "data";
  static final String CODE = "code";
  static final String pxcxCode = "";
  static var codes = {0, 1, 200};

  static Utils _instance;

  static Utils getInstance() {
    if (_instance == null) {
      _instance = Utils();
    }
    return _instance;
  }

  // 学时转换
  convertToPeriodHour(int time) {
    int hour = time ~/ 45;
    int minutes = time - hour * 45;
    if (time <= 0) {
      hour = 0;
      minutes = 0;
    } else {
      hour = time ~/ 45;
      minutes = time - hour * 45;
    }

    return '$hour学时$minutes分钟';
  }

  // 判空
  checkEmpty(String s) {
    if (s == null || s.isEmpty) {
      return true;
    }
    return false;
  }

  getSha256(String msg, String pass) {
    var bytes = utf8.encode(msg);
    var key = utf8.encode(pass);
    var hmacSha256 = new Hmac(sha256, key);
    var digest = hmacSha256.convert(bytes);
//    print("HMAC digest as bytes: ${digest.bytes}");
    return '$digest';
  }

  getSha1(String msg) {
    var bytes = utf8.encode(msg); // data being hashed
    return '${sha1.convert(bytes)}';
  }

  //升级
/*  Future<Null> updateApp(String url, bool cancelAble) async {
    if (Platform.isIOS) {
//      final url = APPLE_STORE; // id 后面的数字换成自己的应用 id 就行了
//      if (await canLaunch(url)) {
//        await launch(url, forceSafariVC: false);
//      } else {
//        throw 'Could not launch $url';
//      }
      String result = await jumpPlugin.invokeMethod('viewVersionNumber');
      print(result);
      if (result == 'successToConnect') {
        print('收到返回信息');
      }
    } else if (Platform.isAndroid) {
      Map<String, dynamic> jsonData = Map();
      jsonData['url'] = url;
      jsonData['cancelAble'] = cancelAble;
      String result = await jumpPlugin.invokeMethod('update', jsonData);
      print(result);
      if (result == 'successToConnect') {
        print('收到返回信息');
      }
    }
  }*/

  showSnackBar(var scaffoldkey, String data) {
    scaffoldkey.currentState.showSnackBar(SnackBar(
        elevation: 0,
        backgroundColor: gold,
        content: Text(
          data,
          style: TextStyle(fontSize: 18),
        )));
  }

  // 解析扫描二维码 获得蓝牙名称
  getTerminalId(String s) {
    s = s.trim();
    if (s.startsWith('start') && s.endsWith('end')) {
      print(s.split('<')[1].split(':')[0]);
      return s.split('<')[1].split(':')[0];
    }
  }

  //格式化时间
  formatDuration(int secondsPassed) {
    //  ～/ 取整操作
    int seconds = secondsPassed % 60;
    int minutes = secondsPassed ~/ 60;
    int hours = secondsPassed ~/ (60 * 60);
    return secondsPassed == 0 ? '' : '本次已观看$hours小时$minutes分$seconds秒';
  }

  // 检查是否登录
  Future<int> checkLogin() async {
    // SharedPreferences sp = await SharedPreferences.getInstance();
    // if (checkEmpty(sp.getString(UUID))) {
    //   //未登录
    //   return UNSIGN;
    // } else {
    //   //登录
    //   if (checkEmpty(sp.getString(PERSON_NUM))) {
    //     // 未绑定身份证
    //     return UNBIND;
    //   } else {
    //     return BIND;
    //   }
    // }
  }

  showBottomSheet(BuildContext context, String title, List<Widget> items) {
    showModalBottomSheet(
        backgroundColor: black,
        context: context,
        builder: (BuildContext context) {
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  title,
                  style: TextStyle(color: gold, fontSize: 16),
                ),
              ),
              Divider(
                color: gold,
                height: 1,
              ),
              Column(
                children: items,
              ),
              ListTile(
                title: Text(
                  '否',
                  style: TextStyle(color: gold),
                ),
                onTap: () {
                  Navigator.pop(context);
                },
              )
            ],
          );
        });
  }
}
